unit cuMath64;

interface

 //Author John O'Harrow
 //Original name FloorDoubleJOH
function Ceil64(const X : Extended) : Int64; overload;
function Ceil64(const X : Double) : Int64; overload;
function Ceil64(const X : Single) : Int64; overload;

function Floor64(const X : Extended) : Int64; overload;
function Floor64(const X : Double) : Int64; overload;
function Floor64(const X : Single) : Int64; overload;

 //Author Daniel Avis
function toRadians(const newDegrees:extended):real;
function toDegrees(const newRadians:extended):real;

implementation

 function Ceil64(const X : Extended) : Int64; overload;
 var
   OldCW, NewCW : Word;
 asm
   fnstcw OldCW {Save Current Control Word}
   mov ax, OldCW
   and ax, $FBFF {Clear Bit 10}
   or ax, $0B00 {Set Bits 8, 9 and 11}
   mov NewCW, ax {Bits 8/9 = Precision, Bits 10/11 = Rounding Mode}
   fldcw NewCW {Set Round to +ve Infinity Mode with 64 Bit Precision}
   fld X
   fistp Result
   fldcw OldCW {Restore Original Control Word}
 end;

 function Ceil64(const X : Double) : Int64; overload;
 var
   OldCW, NewCW : Word;
 asm
   fnstcw OldCW {Save Current Control Word}
   mov ax, OldCW
   and ax, $FBFF {Clear Bit 10}
   or ax, $0B00 {Set Bits 8, 9 and 11}
   mov NewCW, ax {Bits 8/9 = Precision, Bits 10/11 = Rounding Mode}
   fldcw NewCW {Set Round to +ve Infinity Mode with 64 Bit Precision}
   fld X
   fistp Result
   fldcw OldCW {Restore Original Control Word}
 end;

 function Ceil64(const X : Single) : Int64; overload;
 var
   OldCW, NewCW : Word;
 asm
   fnstcw OldCW {Save Current Control Word}
   mov ax, OldCW
   and ax, $FBFF {Clear Bit 10}
   or ax, $0B00 {Set Bits 8, 9 and 11}
   mov NewCW, ax {Bits 8/9 = Precision, Bits 10/11 = Rounding Mode}
   fldcw NewCW {Set Round to +ve Infinity Mode with 64 Bit Precision}
   fld X
   fistp Result
   fldcw OldCW {Restore Original Control Word}
 end;

function Floor64(const X : Extended) : Int64; overload;
var
  OldCW, NewCW : Word;
asm
  fnstcw OldCW {Save Current Control Word}
  mov ax, OldCW
  and ax, $F7FF {Clear Bit 11}
  or ax, $0700 {Set Bits 8, 9 and 10}
  mov NewCW, ax {Bits 8/9 = Precision, Bits 10/11 = Rounding Mode}
  fldcw NewCW {Set Round to -ve Infinity Mode with 64 Bit Precision}
  fld X
  fistp Result
  fldcw OldCW {Restore Original Control Word}
end;

function Floor64(const X : Double) : Int64; overload;
var
   OldCW, NewCW : Word;
asm
  fnstcw OldCW {Save Current Control Word}
  mov ax, OldCW
  and ax, $F7FF {Clear Bit 11}
  or ax, $0700 {Set Bits 8, 9 and 10}
  mov NewCW, ax {Bits 8/9 = Precision, Bits 10/11 = Rounding Mode}
  fldcw NewCW {Set Round to -ve Infinity Mode with 64 Bit Precision}
  fld X
  fistp Result
  fldcw OldCW {Restore Original Control Word}
end;

function Floor64(const X : Single) : Int64; overload;
var
  OldCW, NewCW : Word;
asm
  fnstcw OldCW {Save Current Control Word}
  mov ax, OldCW
  and ax, $F7FF {Clear Bit 11}
  or ax, $0700 {Set Bits 8, 9 and 10}
  mov NewCW, ax {Bits 8/9 = Precision, Bits 10/11 = Rounding Mode}
  fldcw NewCW {Set Round to -ve Infinity Mode with 64 Bit Precision}
  fld X
  fistp Result
  fldcw OldCW {Restore Original Control Word}
end;

function toRadians(const newDegrees:extended):real;
begin
  result := Pi/180 * newDegrees;
end;
function toDegrees(const newRadians:extended):real;
begin
  result := 180/Pi * newRadians;
end;


end.
